#!/bin/bash

Install_Kafka() {
  Echo_Info "====== Installing Kafka ======"
  Echo_Info "Install ${Kafka_Ver} Version..."
  Press_Start
  cd ${cur_dir}/src

  # TODO 判断是否已经安装了Kafka

  # 下载安装包并解压
  Download_Files ${YUSP_Download_Mirror}/kafka_${Kafka_Ver}.tgz kafka_${Kafka_Ver}.tgz
  Tar_Cd kafka_${Kafka_Ver}.tgz kafka_${Kafka_Ver}
  pwd
  cd ${cur_dir}/src
  # 拷贝到安装目录
  echo "mv kafka_${Kafka_Ver} to /usr/local ..."
  rm -rf /usr/local/kafka*
  mv ./kafka_${Kafka_Ver} /usr/local/

  # 修改配置文件
  cd /usr/local/kafka_${Kafka_Ver}/config
  mkdir /usr/local/kafka_${Kafka_Ver}/kafka-logs/
  sed -i "s#/tmp/kafka-logs/#/usr/local/kafka_${Kafka_Ver}/kafka-logs/#" server.properties

  # kafka
  /usr/local/kafka_${Kafka_Ver}/bin/kafka-server-start.sh /usr/local/kafka_${Kafka_Ver}/config/server.properties &

  if [ $? -eq 0 ]; then
    Echo_Green "OK, ====== Kafka_Install Successful!!======"
  else
    Echo_Red "Sorry, Kafka_Install failed! please check you operation and try again!"
    return -1
  fi

}

Uninstall_Kafka() {
  echo "You will uninstall Kafka..."
  echo "Shutting down Kafka..."
  # 杀掉zookeeper进程
  kafka_pid=`ps -ef|grep kafka | grep -v color|awk '{print $2}'`
  kill $kafka_pid
  # 删除安装文件
  rm -rf /usr/local/kafka*

  Echo_Green "Uninstall Kafka completed."
}
